﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.ResourceRequest.Services;
using ePlanet.ResourceRequest.DataAccess;
using ePlanet.ResourceRequest.Models;

namespace Web.Views.TempUserManage
{
    public partial class RegistTempUser : System.Web.UI.Page
    {
        [Dependency]
        public ITempUserService m_ITempUserService
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRegistTempUser_Click(object sender, EventArgs e)
        {
            try
            {
                CheckDataNull();
                TempUser userModel = new TempUser();
                TempUser.TEMPUSERRow currentUser = userModel.TEMPUSER.NewTEMPUSERRow();
                currentUser.LOGINNAME = this.txtLoginName.Text.Trim();
                if (this.m_ITempUserService.CheckUserLoginName(currentUser.LOGINNAME))
                {
                    throw new Exception("存在登录名为：" + currentUser.LOGINNAME + " 的用户");
                }
                currentUser.USERNAME = this.txtUserName.Text.Trim();
                currentUser.APPLYTIME = DateTime.Now;
                currentUser.EXPIRATIONTIME = DateTime.Parse(this.txtExpirationTime.Text.Trim());
                currentUser.IDCARD = this.txtIDCard.Text.Trim();
                currentUser.ORGNAME = this.txtOrgName.Text.Trim();
                if (this.txtPassword.Text.Trim() != this.txtPassword2.Text.Trim())
                {
                    throw new Exception("两次输入的密码不一致");
                }
                else
                {
                    currentUser.USERPASSWORD = this.txtPassword.Text.Trim();
                }
                currentUser.TEMPUSERID = Guid.NewGuid().ToString();
                currentUser.MOBILE = this.txtMobile.Text.Trim();
                currentUser.TELPHONE = this.txtTelphone.Text.Trim();

                userModel.TEMPUSER.AddTEMPUSERRow(currentUser);

                bool result = this.m_ITempUserService.AddNewTempUser(userModel);

                if (result)
                {
                    Response.Redirect("success.htm");
                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(this.GetType()
                    , "btnRegistTempUser_Click"
                    , string.Format("<script>alert(\"" + ex.Message + "\")</script>"));
            }
        }

        private bool CheckDataNull()
        {
            bool rtnBool = false;

            this.CheckDataNull(this.txtExpirationTime, "过期时间不能为空");
            this.CheckDataNull(this.txtIDCard, "身份证号码不能为空");
            this.CheckDataNull(this.txtLoginName, "登录名不能为空");
            this.CheckDataNull(this.txtMobile, "手机号码不能为空");
            this.CheckDataNull(this.txtOrgName, "组织机构不能为空");
            this.CheckDataNull(this.txtPassword, "密码不能为空");
            this.CheckDataNull(this.txtPassword2, "重复密码不能为空");
            this.CheckDataNull(this.txtTelphone, "电话号码不能为空");
            this.CheckDataNull(this.txtUserName, "真实姓名不能为空");

            return rtnBool;
        }

        private void CheckDataNull(TextBox txt, string message)
        {
            if (string.IsNullOrEmpty(txt.Text))
            {
                throw new Exception(message);
            }
        }
    }
}